#include "include/planesystem2.h"
#include <QGridLayout>
#include <QVBoxLayout>
#include <cstdio>
#include <QFormLayout>


//窗口初始化设计
void PlaneSystem2::InitWindows() {
    // 设计窗口标题
    this->setWindowTitle("飞机订票系统");     

    // 隐藏菜单栏
    QMenuBar* menuBar = this->menuBar();
    if (menuBar) {
        menuBar->hide();     
    }

    // 隐藏工具栏
    foreach(QToolBar * toolBar, this->findChildren<QToolBar*>()) {
        if (toolBar) {
            toolBar->hide();
        }
    }
     
    // 固定窗口大小
    this->WindowHeight = 600;
    this->WindowWidth = 1000;
    this->setFixedSize(WindowWidth, WindowHeight);

    // 编辑窗口组件： 
    // 应用标题
    QLabel*titleLable = new QLabel(this);
    titleLable->setText("航空订票系统");
    titleLable->setObjectName("title");
    titleLable->setAlignment(Qt::AlignCenter);
    titleLable->setGeometry(0, 20, WindowWidth, 54);
    titleLable->setStyleSheet(cssStyle->getTitleLableStyle());
    
    
}


//登录按钮制作
void PlaneSystem2::LoginButton() {
    // 制作一个圆形的按钮，第一次点击按钮是检查是否登录，
    //如果没有登录就显示登录页面，已经登录后再点击该按钮就在侧边显示一个小页面（页面只有主页面的1/4，并且页面右侧紧贴主页面的右侧
    //一旦点击主页面后，该小页面就消失）并且圆形按钮可以实现上传用户头像

    // 创建一个圆形按钮
    loginButton = new QPushButton(this);
    loginButton->setGeometry(900, 20, 54, 54); // 设置按钮位置
    loginButton->setStyleSheet(R"(
    QPushButton { background-color: lightgray; border: 2px solid black; font-weight: bold; }
    QPushButton:hover {
        background: white; /* 鼠标悬浮时的背景色 */
    })");
    loginButton->setText("点击登录");

    connect(loginButton, &QPushButton::clicked, this, &PlaneSystem2::onLoginButtonClicked);
}

// 侧方面板设计
void PlaneSystem2::InitSidePanel() {
    // 创建侧边面板，但初始状态隐藏
    sidePanel = new QWidget(this);
    sidePanel->setFixedSize(WindowWidth / 4, WindowHeight);
    sidePanel->move(WindowWidth, 0); // 将其定位在主窗口外部
    sidePanel->setStyleSheet("background-color: lightgray;");
    sidePanel->installEventFilter(this);// 在初始化侧边屏幕时设置事件过滤器
    sidePanel->hide();

    // 点击主屏隐藏侧屏
    connect(this, &PlaneSystem2::mainScreenClicked, this, &PlaneSystem2::onMainWindowClicked);

    // 在侧边面板内创建一个标签用于显示头像
    avatarButton = new QPushButton(sidePanel);
    avatarButton->setGeometry(20, 10, 54, 54); // 调整大小和位置
    avatarButton->setStyleSheet("border: 2px solid black; ");

    connect(avatarButton, &QPushButton::clicked, this, &PlaneSystem2::onAvatarButtonClicked);

    // 创建一个lable显示用户名称
    clientNameLabel = new QLabel(sidePanel);
    clientNameLabel->setStyleSheet("QLabel {font-weight: bold; font-size: 16px}");
    clientNameLabel->setText("用户名");
    clientNameLabel->setGeometry(90,25,80,30);
    clientNameLabel->setMinimumSize(QSize(80, 30)); 
    clientNameLabel->setMaximumSize(QSize(200, 30));
    clientNameLabel->setTextInteractionFlags(Qt::TextSelectableByMouse);

    // 创建功能性按钮
    /*
    * 修改个人信息按钮
    - 预订信息按钮
    - 退订信息按钮
    */
    clientMegButton = new QPushButton(sidePanel);
    clientMegButton->setText("个人信息");
    clientMegButton->setGeometry(50, 125, 150, 25);
    clientMegButton->setStyleSheet(cssStyle->getButtonStyle());

    connect(clientMegButton,&QPushButton::clicked,this, &PlaneSystem2::onClientMegButtonClicked);

    bookingButton = new QPushButton(sidePanel);
    bookingButton->setText("预订机票");
    bookingButton->setGeometry(50, 175, 150, 25);
    bookingButton->setStyleSheet(cssStyle->getButtonStyle());

    connect(bookingButton, &QPushButton::clicked, this, &PlaneSystem2::onBookingButtonClicked);

    refundButton = new QPushButton(sidePanel);
    refundButton->setText("退订机票");
    refundButton->setGeometry(50, 225, 150, 25);
    refundButton->setStyleSheet(cssStyle->getButtonStyle());
}

// 更多功能按钮制作
void PlaneSystem2::MoreFunButton() {
    moreFunButton = new QPushButton(this);
    moreFunButton->setGeometry(0, 33, 30, 30); // 设置按钮位置
    moreFunButton->setStyleSheet(cssStyle->getMoreFunButton());
    moreFunButton->setText("✈️");

    connect(moreFunButton,&QPushButton::clicked, this, &PlaneSystem2::onMoreFunButtonClicked);
}

void PlaneSystem2::InitMoreFunSidePanel(){
    // 创建侧边面板，但初始状态隐藏
    moreFunsidePanel = new QWidget(this);
    moreFunsidePanel->setFixedSize(WindowWidth / 6, WindowHeight);
    moreFunsidePanel->move(WindowWidth, 0); // 将其定位在主窗口外部
    moreFunsidePanel->setStyleSheet("background-color: lightgray;");
    moreFunsidePanel->installEventFilter(this);// 在初始化侧边屏幕时设置事件过滤器
    moreFunsidePanel->hide();

    // 点击主屏隐藏侧屏
    connect(this, &PlaneSystem2::mainScreenClicked, this, &PlaneSystem2::onMainWindowClicked);

    // 功能性按钮
}

// 查看航票信息面板
void PlaneSystem2::InitTicketBookWindow() {
    // 主窗口
    ticketBookWindow = new QWidget(this);
    ticketBookWindow->setFixedSize(WindowWidth-20,440);
    ticketBookWindow->move(10, 150);
    ticketBookWindow->setStyleSheet(cssStyle->getTicketBookStyle());
    cssStyle->addShadowEffect(ticketBookWindow);
    
    // 获取所有航班信息
    trip = new Trip();
    vector<Trip> trips = trip->SearchAllTrip();

    // 网格布局
    QGridLayout* QGLayout = new QGridLayout;
    // 添加按钮到网格布局
    for (int i = 0; i < trips.size(); ++i) {
        int row = i / 5; // 假设每行5个按钮
        int col = i % 5;

        QString adress = QString::fromStdString(trips[i].getTakeOffCity() + " -> " + trips[i].getFallCity());
        QString time = QString::fromStdString(trips[i].getTakeOffTime());
        char buffer[50];
        sprintf_s(buffer, "%.2f", trips[i].getPrice());
        std::string str = buffer;
        QString price = QString::fromStdString("💰"+str);

        QPushButton* btn;
        QGLayout->addWidget(btn = TicketMsgButton(ticketBookWindow,adress, time, price),row,col);

        btn->setProperty("trip", QVariant::fromValue(trips[i]));
        // 槽函数
        connect(btn, &QPushButton::clicked, this, &PlaneSystem2::onTicketMsgButtonClick);
    }
    // 添加布局
    ticketBookWindow->setLayout(QGLayout);
}

// 查看订票信息按钮制作
QPushButton* PlaneSystem2::TicketMsgButton(QWidget* parent,QString address, QString time, QString price) {
    QPushButton*btn = new QPushButton(parent);
    btn->setFixedSize(150,125);
    btn->setStyleSheet(cssStyle->getTickerMsgButton());
    InitTicketMegButtonPanel(btn, address, time, price);
    cssStyle->addShadowEffect(btn);

    return btn;
}  

// 订票信息按钮信息面板
void PlaneSystem2::InitTicketMegButtonPanel(QPushButton* parent,QString address,QString time,QString price){
    QWidget* panel = new QWidget(parent);
    panel->setFixedSize(150,125);
    
    
    // 创建Label
    QLabel* addressLabel = new QLabel(panel);
    QLabel* timeLabel = new QLabel(panel);
    QLabel* priceLabel = new QLabel(panel);

    addressLabel->setText(address);
    timeLabel->setText(time);
    priceLabel->setText(price);



    // 设置布局
    QVBoxLayout* QVLayout = new QVBoxLayout();
    QVLayout->addWidget(addressLabel);
    QVLayout->addWidget(timeLabel);
    QVLayout->addWidget(priceLabel);

    // 添加布局
    panel->setLayout(QVLayout);
}

// 个人信息面板
void PlaneSystem2::InitClientMegButtonPanel() {
    // 面板制作
    clientMegPanel = new QWidget(this);
    clientMegPanel->setFixedSize(300, 400);
    clientMegPanel->setStyleSheet(cssStyle->getTicketBookStyle());
    clientMegPanel->move((this->width() - clientMegPanel->width()) / 2, (this->height() - clientMegPanel->height()) / 2);
    cssStyle->addShadowEffect(clientMegPanel);

    // 个人信息面板
    QWidget* showClientPanel = new QWidget(clientMegPanel);
    showClientPanel->setFixedSize(280, 300);
    showClientPanel->move(10,100);

    // Titlelabel制作
    QLabel* titleLabel = new QLabel(clientMegPanel);
    titleLabel->setText("个人信息");
    titleLabel->setObjectName("title");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setGeometry(0, 25, 300, 50);
    titleLabel->setStyleSheet(R"(QLabel#title {font-size: 30px; font-weight: bold; color: black; text-align: center; })");

    // 退出按钮的制作
    QPushButton* clientMegCloseButton = new QPushButton(clientMegPanel);
    clientMegCloseButton->setGeometry(270,10,20,20);
    clientMegCloseButton->setText("×");
    clientMegCloseButton->setStyleSheet(cssStyle->getCloseButtonStyle());

    connect(clientMegCloseButton,&QPushButton::clicked,this,&PlaneSystem2::onClientMegButtonCloseClicked);

    // 个人信息Label制作
    QLabel* idLabel = new QLabel(showClientPanel);
    idLabel->setObjectName("clientMegLabel");
    idLabel->setText("Id：");
    QLabel* idValueLabel = new QLabel(showClientPanel);
    idValueLabel->setObjectName("clientMegLabel");
    idValueLabel->setText(QString::number(currentClient->getClientId()));
    
    QLabel* nameLabel = new QLabel(showClientPanel);
    nameLabel->setObjectName("clientMegLabel");
    nameLabel->setText("姓名：");
    QLabel* nameValueLabel = new QLabel(showClientPanel);
    nameValueLabel->setObjectName("clientMegLabel");
    nameValueLabel->setText(QString::fromStdString(currentClient->getClientName()));

    QLabel* phoneLabel = new QLabel(showClientPanel);
    phoneLabel->setObjectName("clientMegLabel");
    phoneLabel->setText("电话：");
    QLabel* phoneValueLabel = new QLabel(showClientPanel);
    phoneValueLabel->setObjectName("clientMegLabel");
    phoneValueLabel->setText(QString::fromStdString(currentClient->getClientPhone()));

    // 标签样式
    idLabel->setStyleSheet(cssStyle->getClientMegLabelSstyle());
    idValueLabel->setStyleSheet(cssStyle->getClientMegLabelSstyle());
    nameLabel->setStyleSheet(cssStyle->getClientMegLabelSstyle());
    nameValueLabel->setStyleSheet(cssStyle->getClientMegLabelSstyle());
    phoneLabel->setStyleSheet(cssStyle->getClientMegLabelSstyle());
    phoneValueLabel->setStyleSheet(cssStyle->getClientMegLabelSstyle());

    // 表单布局
    QFormLayout* formLayout = new QFormLayout(showClientPanel);

    formLayout->addRow(idLabel, idValueLabel);
    formLayout->addRow(nameLabel, nameValueLabel);
    formLayout->addRow(phoneLabel, phoneValueLabel);

    formLayout->setSpacing(20); // 设置行之间的间距为20像素
    
    clientMegPanel->show();
}

                      
// 航班信息面板
void PlaneSystem2::InitTicketMegButtonPanel(const Trip& trip) {
    // 面板制作
    ticketMegPanel = new QWidget(this);
    ticketMegPanel->setFixedSize(300, 450);
    ticketMegPanel->setStyleSheet(cssStyle->getTicketBookStyle());
    ticketMegPanel->move((this->width() - ticketMegPanel->width()) / 2, (this->height() - ticketMegPanel->height()) / 2);
    cssStyle->addShadowEffect(ticketMegPanel);

    // 个人信息面板
    QWidget* showTripPanel = new QWidget(ticketMegPanel);
    showTripPanel->setFixedSize(280, 300);
    showTripPanel->move(10, 80);

    // Titlelabel制作
    QLabel* titleLabel = new QLabel(ticketMegPanel);
    titleLabel->setText("航班信息");
    titleLabel->setObjectName("title");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setGeometry(0, 25, 300, 50);
    titleLabel->setStyleSheet(R"(QLabel#title {font-size: 24px; font-weight: bold; color: black; text-align: center; })");

    // 退出按钮的制作
    QPushButton* clientMegCloseButton = new QPushButton(ticketMegPanel);
    clientMegCloseButton->setGeometry(270, 10, 20, 20);
    clientMegCloseButton->setText("×");
    clientMegCloseButton->setStyleSheet(cssStyle->getCloseButtonStyle());

    connect(clientMegCloseButton, &QPushButton::clicked, this, &PlaneSystem2::onTicketMegButtonCloseClicked);

    // 获取航班飞机信息
    Plane* plane = new Plane();
    plane->SearchPlane(trip.getPlaneId());

    // 航班信息标签制作
    vector<vector<QLabel*>> result;

    result.push_back(MakeInfoLabel(showTripPanel,cssStyle,"TripId：",QString::number(trip.getTripId())));
    result.push_back(MakeInfoLabel(showTripPanel,cssStyle,"PlaneId：",QString::number(plane->getPlaneId())));
    result.push_back(MakeInfoLabel(showTripPanel,cssStyle,"飞机型号：",QString::fromStdString(plane->getPlaneModel())));
    result.push_back(MakeInfoLabel(showTripPanel,cssStyle,"飞机座位数：", QString::number(plane->getSeatSize())));
    result.push_back(MakeInfoLabel(showTripPanel,cssStyle,"出发时间：", QString::fromStdString(trip.getTakeOffTime())));
    result.push_back(MakeInfoLabel(showTripPanel,cssStyle,"抵达时间：", QString::fromStdString(trip.getFallTime())));
    result.push_back(MakeInfoLabel(showTripPanel,cssStyle,"出发地 -> 目的地：", QString::fromStdString(trip.getTakeOffCity()+" -> "+trip.getFallCity())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "机票价格💰：", QString::number(trip.getPrice())));

    // 表单布局
    QFormLayout* formLayout = new QFormLayout(showTripPanel);

    for (int i = 0; i < result.size(); i++)
    {
        formLayout->addRow(result[i][0], result[i][1]);
    }
    
    formLayout->setSpacing(20); // 设置行之间的间距为20像素

    // 预定按钮制作
    QPushButton* bookButton = new QPushButton(ticketMegPanel);
    bookButton->setGeometry(110,395,80,35);
    bookButton->setText("点击预订");
    bookButton->setStyleSheet(cssStyle->getBookButtonStyle());

    bookButton->setProperty("tripId", QVariant::fromValue(trip.getTripId()));
    connect(bookButton,&QPushButton::clicked,this,&PlaneSystem2::onBookButtonClicked);

    // 面板显示
    ticketMegPanel->show();
}

// 个人信息-订票信息面板
void PlaneSystem2::InitClientMegBookingTripButtonPanel() {
    bookTripPanel = new QWidget(this);
    bookTripPanel->setFixedSize(WindowWidth - 200, 440);
    bookTripPanel->move(100, 150);
    bookTripPanel->setStyleSheet(cssStyle->getTicketBookStyle());
    cssStyle->addShadowEffect(bookTripPanel);

    // 退出按钮的制作
    QPushButton* CloseButton = new QPushButton(bookTripPanel);
    CloseButton->setGeometry(bookTripPanel->width()-40, 10, 20, 20);
    CloseButton->setText("×");
    CloseButton->setStyleSheet(cssStyle->getCloseButtonStyle());

    connect(CloseButton, &QPushButton::clicked, this, &PlaneSystem2::onClientMegBookingTripButtonClicked);

    // 刷新按钮制作
    QPushButton* FlashButton = new QPushButton(bookTripPanel);
    FlashButton->setGeometry(bookTripPanel->width() - 60, bookTripPanel->height()-60, 35, 35);
    FlashButton->setIcon(QIcon("./res/other/flash.png"));
    FlashButton->setIconSize(QSize(35, 35));
    FlashButton->setStyleSheet(cssStyle->getFlashButtonStyle());

    //connect(FlashButton, &QPushButton::clicked, this, &PlaneSystem2::onFlashButtonClicked);

    // 获取所有的订单
    vector<Book> books = currentClient->SearchAllBookMsg();
    
    QGridLayout* QGLayout = new QGridLayout;
    // 添加按钮到网格布局
    for (int i = 0; i < books.size(); ++i) {
        int row = i / 5; // 假设每行5个按钮
        int col = i % 5;

        // 获取具体的Trip信息
        Trip* trip = new Trip();
        trip->SearchTrip(books[i].getTripId());

        // 制作板块
        QString adress = QString::fromStdString(trip->getTakeOffCity() + " -> " + trip->getFallCity());
        QString time = QString::fromStdString(trip->getTakeOffTime());
        char buffer[50];
        sprintf_s(buffer, "%.2f", trip->getPrice());
        std::string str = buffer;
        QString price = QString::fromStdString("💰" + str);

        QPushButton* btn;
        QGLayout->addWidget(btn = TicketMsgButton(bookTripPanel,adress, time, price), row, col);

        // 使用数组包裹可以避免注册问题
        vector<Trip> trips;
        trips.push_back(*trip);

        btn->setProperty("trip", QVariant::fromValue(trips[0]));
        btn->setProperty("time", QVariant::fromValue(books[i].getParseBookTime()));
        // 槽函数
        connect(btn, &QPushButton::clicked, this, &PlaneSystem2::onBookTicketMsgButtonClick);
    }

    bookTripPanel->setLayout(QGLayout);
    bookTripPanel->show();
}

// 个人信息-航票信息面板
void  PlaneSystem2::InitBookTicketMegButtonPanel(const Trip& trip,string time) {
    // 面板制作
    bookTicketMegPanel = new QWidget(this);
    bookTicketMegPanel->setFixedSize(300, 450);
    bookTicketMegPanel->setStyleSheet(cssStyle->getTicketBookStyle());
    bookTicketMegPanel->move((this->width() - bookTicketMegPanel->width()) / 2, (this->height() - bookTicketMegPanel->height()) / 2);
    cssStyle->addShadowEffect(bookTicketMegPanel);

    // 个人信息面板
    QWidget* showTripPanel = new QWidget(bookTicketMegPanel);
    showTripPanel->setFixedSize(280, 300);
    showTripPanel->move(10, 80);

    // Titlelabel制作
    QLabel* titleLabel = new QLabel(bookTicketMegPanel);
    titleLabel->setText("航班信息");
    titleLabel->setObjectName("title");
    titleLabel->setAlignment(Qt::AlignCenter);
    titleLabel->setGeometry(0, 25, 300, 50);
    titleLabel->setStyleSheet(R"(QLabel#title {font-size: 24px; font-weight: bold; color: black; text-align: center; })");

    // 退出按钮的制作
    QPushButton* clientMegCloseButton = new QPushButton(bookTicketMegPanel);
    clientMegCloseButton->setGeometry(270, 10, 20, 20);
    clientMegCloseButton->setText("×");
    clientMegCloseButton->setStyleSheet(cssStyle->getCloseButtonStyle());

    connect(clientMegCloseButton, &QPushButton::clicked, this, &PlaneSystem2::onBookTicketMegButtonCloseClicked);

    // 获取航班飞机信息
    Plane* plane = new Plane();
    plane->SearchPlane(trip.getPlaneId());

    // 航班信息标签制作
    vector<vector<QLabel*>> result;

    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "TripId：", QString::number(trip.getTripId())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "PlaneId：", QString::number(plane->getPlaneId())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "飞机型号：", QString::fromStdString(plane->getPlaneModel())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "飞机座位数：", QString::number(plane->getSeatSize())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "出发时间：", QString::fromStdString(trip.getTakeOffTime())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "抵达时间：", QString::fromStdString(trip.getFallTime())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "出发地 -> 目的地：", QString::fromStdString(trip.getTakeOffCity() + " -> " + trip.getFallCity())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "机票价格💰：", QString::number(trip.getPrice())));
    result.push_back(MakeInfoLabel(showTripPanel, cssStyle, "预订时间：", QString::fromStdString(time)));

    // 表单布局
    QFormLayout* formLayout = new QFormLayout(showTripPanel);

    for (int i = 0; i < result.size(); i++)
    {
        formLayout->addRow(result[i][0], result[i][1]);
    }

    formLayout->setSpacing(20); // 设置行之间的间距为20像素

    // 退订按钮制作
    QPushButton* Button = new QPushButton(bookTicketMegPanel);
    Button->setGeometry(110, 395, 80, 35);
    Button->setText("退订");
    Button->setStyleSheet(cssStyle->getBookButtonStyle());

    Button->setProperty("tripId", QVariant::fromValue(trip.getTripId()));
    connect(Button, &QPushButton::clicked, this, &PlaneSystem2::onRefundButtonClicked);

    // 面板显示
    bookTicketMegPanel->show();
}



/*功能函数*/

// 标签制作
vector<QLabel*> MakeInfoLabel(QWidget* parent,CssStyle* cssStyle,QString key,QString value) {
    QLabel* keyLabel = new QLabel(parent);
    keyLabel->setText(key);
    QLabel* valueLabel = new QLabel(parent);
    valueLabel->setText(value);
    
    // 标签样式

    vector<QLabel*> result;
    result.push_back(keyLabel);
    result.push_back(valueLabel);

    return result;
}